package com.dongbo.recorder.screenrecord.base;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.Fragment;

import com.dongbo.recorder.screenrecord.utils.eventbus.EventBusUtils;
import com.tbruyelle.rxpermissions3.RxPermissions;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import butterknife.ButterKnife;

/**
 * @author GuoZi
 * @date :2020/5/5 12:18
 * @description:
 */
public abstract class BaseFragment extends Fragment {
    public RxPermissions rxPermissions;

    private Context mContext;
    private boolean isFirstLoad = true; // 是否第一次加载

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(getLayoutId(), null);
        ButterKnife.bind(this,view);
        rxPermissions = new RxPermissions(this);
        initView(view,savedInstanceState);
        if(useEventBus()){
            EventBusUtils.register(this);
        }
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFirstLoad = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstLoad) {
            // 将数据加载逻辑放到onResume()方法中
            initData();
            initListener();
            isFirstLoad = false;
        }
    }

    //布局
    protected abstract int getLayoutId();

    //初始化view
    protected abstract void initView(View view, Bundle savedInstanceState);

    //初始化data
    protected abstract void initData();

    //点击事件
    protected abstract void initListener();

    //是否启用eventbus
    public boolean useEventBus() {
        return false;
    }


    /**
     * 显示toast
     *
     * @param msg
     */
    public void showToast(String msg) {
        if (!isNotificationEnabled(getContext())) {
//            ToastUtils.show(msg);
            Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
        } else {
//            showSToast(text);
            Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
        }
    }


    private boolean isNotificationEnabled(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo appInfo = context.getApplicationInfo();
            String pkg = context.getApplicationContext().getPackageName();
            int uid = appInfo.uid;
            Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
            try {
                appOpsClass = Class.forName(AppOpsManager.class.getName());
                Method checkOpNoThrowMethod = appOpsClass.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class);
                Field opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION");
                int value = (int) opPostNotificationValue.get(Integer.class);
                return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            NotificationManagerCompat mNm = NotificationManagerCompat.from(context);
            return mNm.areNotificationsEnabled();
        }
        return true;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //注销eventbus
        if(useEventBus()){
            EventBusUtils.unregister(this);
        }
    }
}
